package com.zhuss.lock;

/**
 * 分布式锁 key生成工具类
 *
 * @Author zhushanshuo
 * @Date 2024-08-07 10:51:15
 */
public class LockKeyUtil {

    public static final Integer 加锁获取线程 = 1;

    public static final Integer 释放锁获取线程 = -1;



    /**
     * 虚拟的分布式线程ID 的key
     */
    public static final String CLUSTER_THREAD_ID_KEY = "CLUSTER_THREAD_ID";


    /**
     * {}: 为了解决 Cluster Redis 集群模式下，动态slot分配，让 {Lock} 参与 Hash 槽运算，让其分布在相同的槽位
     *      便于WatchDog 对活跃的锁 做 keys {Lock}* 查询， 给锁续命
     * 锁前缀
     */
    public static final String LOCK_PRE_STR = "{Lock}";

    /**
     * keys 命令，查看所有的锁 key
     */
    public static final String LOCK_PRE_KEYS = LOCK_PRE_STR + "*";


    /**
     * Lock:System:Module:Action:ServicePkId
     */
    public static final String KEY_PATTERN = LOCK_PRE_STR + ":%s:%s:%s:%s";

    public static final String LOCK_VALUE = String.valueOf(1);

    public static final String genKey(LockItem item) {
        return String.format(KEY_PATTERN, item.getSystem(), item.getModule(), item.getAction(), item.getServicePkId());
    }

}
